<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="入网请求单号" prop="requestId">
        <el-input v-model="queryParams.requestId" placeholder="请输入入网请求单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="商户类型" prop="signType">
        <el-select v-model="queryParams.signType" placeholder="请选择商户类型" clearable>
          <el-option label="企业" value="ENTERPRISE" />
          <el-option label="个体户" value="INDIVIDUAL" />
        </el-select>
      </el-form-item>
      <el-form-item label="公司名称" prop="signName">
        <el-input v-model="queryParams.signName" placeholder="请输入公司名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="店铺名称" prop="shortName">
        <el-input v-model="queryParams.shortName" placeholder="请输入店铺名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="法人姓名" prop="legalName">
        <el-input v-model="queryParams.legalName" placeholder="请输入法人姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="联系人手机号" prop="contactMobile">
        <el-input v-model="queryParams.contactMobile" placeholder="请输入联系人手机号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="联系人邮箱" prop="contactEmail">
        <el-input v-model="queryParams.contactEmail" placeholder="请输入联系人邮箱" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属平台" v-if="user.userId === 1" prop="platformNickName">
        <el-input v-model="queryParams.platformNickName" placeholder="请输入所属平台" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属代理" v-if="user.deptId < 101" prop="agentNickName">
        <el-input v-model="queryParams.agentNickName" placeholder="请输入所属代理" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属二级代理" v-if="user.deptId < 102" prop="childAgentNickName">
        <el-input v-model="queryParams.childAgentNickName" placeholder="请输入所属二级代理" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品" prop="productCode">
        <el-select v-model="queryParams.productCode" placeholder="请选择产品" clearable>
          <el-option label="苏商-S0" value="ENTERPRISE_RECHARGE_STANDARD_BANKACCOUNTPAY_SUNINGBANK" />
          <el-option label="厦门-S0" value="ENTERPRISE_RECHARGE_STANDARD_BANKACCOUNTPAY_XIBX" />
          <el-option label="蓝海-S0" value="ENTERPRISE_RECHARGE_STANDARD_BANKACCOUNTPAY_WHLHB" />
          <!-- <el-option label="蓝海-D1" value="ENTERPRISE_RECHARGE_STANDARD_BANKACCOUNTPAY_WHLHB_D1" /> -->
          <!-- <el-option label="蓝海-T3" value="ENTERPRISE_RECHARGE_STANDARD_BANKACCOUNTPAY_WHLHB_T3" /> -->
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['ecs:yeePay:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['ecs:yeePay:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="yeePayList">
      <el-table-column label="店铺名称" fixed="left" width="280" align="center" prop="shortName" />
      <el-table-column label="公司名称" align="center" prop="signName" width="300" />
      <el-table-column label="所属平台" v-if="user.userId === 1" align="center" prop="platformNickName" width="210" />
      <el-table-column label="所属代理" v-if="user.deptId < 101" align="center" prop="agentNickName" width="210" />
      <el-table-column label="所属二级代理" v-if="user.deptId < 102" align="center" prop="childAgentNickName" width="210" />
      <el-table-column label="所属商户" align="center" prop="shopNickName" width="210" />
      <el-table-column label="商户类型 " align="center" prop="signType">
        <template slot-scope="{ row }">
          <span v-if="row.signType === 'INDIVIDUAL'">个体户</span>
          <span v-if="row.signType === 'ENTERPRISE'">企业</span>
        </template>
      </el-table-column>
      <el-table-column label="法人姓名" align="center" prop="legalName" width="110" />
      <el-table-column label="联系人手机号" align="center" prop="contactMobile" width="110" />
      <el-table-column label="联系人邮箱" align="center" prop="contactEmail" width="180" />
      <el-table-column label="结算类型 " align="center" prop="bankAccountType" width="100">
        <template slot-scope="{ row }">
          <span v-if="row.bankAccountType === 'ENTERPRISE_ACCOUNT'">对公</span>
          <span v-else-if="row.bankAccountType === 'DEBIT_CARD'">对私</span>
        </template>
      </el-table-column>
      <el-table-column label="产品" align="center" width="80">
        <template #default="{ row }">
          <span v-if="row.productCode === 'ENTERPRISE_RECHARGE_STANDARD_BANKACCOUNTPAY_WHLHB'">蓝海-S0</span>
          <span v-if="row.productCode === 'ENTERPRISE_RECHARGE_STANDARD_BANKACCOUNTPAY_WHLHB_D1'">蓝海-D1</span>
          <span v-if="row.productCode === 'ENTERPRISE_RECHARGE_STANDARD_BANKACCOUNTPAY_WHLHB_T3'">蓝海-T3</span>
          <span v-if="row.productCode === 'ENTERPRISE_RECHARGE_STANDARD_BANKACCOUNTPAY_SUNINGBANK'">苏商-S0</span>
          <span v-if="row.productCode === 'ENTERPRISE_RECHARGE_STANDARD_BANKACCOUNTPAY_XIBX'">厦门-S0</span>
        </template>
      </el-table-column>
      <el-table-column label="银行虚拟账户" align="center" prop="bankAccountNo" width="230">
        <template #default="{ row }">
          <el-tag type="primary" v-if="row.bankAccountStatus === 0">待提交</el-tag>
          <el-tag type="info" v-if="row.bankAccountStatus === 1">申请中</el-tag>
          <el-tag type="danger" v-if="row.bankAccountStatus === 2">开户失败</el-tag>
          <el-tag type="success" v-if="row.bankAccountStatus === 3">{{ row.bankAccountNo }}</el-tag>
          <el-tag type="primary" v-if="row.bankAccountStatus === 4">待激活</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="入网状态" align="center" prop="status" width="110">
        <template #default="{ row }">
          <el-tag type="primary" v-if="row.status === 1">待提交</el-tag>
          <el-tag type="info" v-else-if="row.status === 2">审核中</el-tag>
          <el-popover v-else-if="row.status === 3" title="驳回原因" :content="row.fail" width="300">
            <el-tag slot="reference" type="danger">审核驳回</el-tag>
          </el-popover>
          <el-tag type="primary" v-else-if="row.status === 4">协议待签署</el-tag>
          <el-tag type="success" v-else-if="row.status === 5">申请完成</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="驳回原因" align="center" prop="fail" width="180" /> -->
      <el-table-column label="更新时间" align="center" prop="createTime" width="180">
        <template #default="{ row }">{{ row.updateTime ? row.updateTime : row.createTime }}</template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="200" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" v-if="scope.row.status === 1 || scope.row.status === 3"
            @click="handleUpdate(scope.row)" v-hasPermi="['ecs:yeePay:edit']">修改</el-button>
          <el-button size="mini" type="text"
            v-if="scope.row.status === 5 || scope.row.status === 2 || scope.row.status === 4 || scope.row.status === 6"
            @click="handleDetail(scope.row)">详情</el-button>
          <el-button size="mini" type="text" v-if="scope.row.status === 5" @click="handleBank(scope.row)"
            v-hasPermi="['ecs:yeePay:edit']">开立银行账户</el-button>
          <el-button size="mini" type="text" @click="feeRateOpen(scope.row)"
            v-if="user.userId === 1 && scope.row.status === 5">修改费率</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <YeePayOperate v-if="open" :title="title" @close="close" :opens="open" :formData="form"></YeePayOperate>
    <YeePayBank v-if="openBank" :requestId="form.requestId" :openBank="openBank" @close="close"
      :productCode="openProductCode" :accountType="openAccountType"></YeePayBank>
    <YeePayDetail v-if="openDetail" @close="close" :opens="openDetail" :id="form.id"></YeePayDetail>

    <el-dialog :title="feeRateTitle" :visible.sync="feeRateVisible" width="35%" append-to-body>
      <el-form ref="feeRateRef" :model="feeRateForm" label-width="110px" class="form">
        <el-form-item label="费率" prop="feeRate">
          <el-input-number :step="0.1" :min="0" :max="100" :precision="2" v-model.number="feeRateForm.feeRate"
            placeholder="请输入费率" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="feeRateSubmit">确 定</el-button>
        <el-button @click="feeRateCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import YeePayOperate from "./modules/operate.vue"
import YeePayBank from "./modules/bank.vue"
import YeePayDetail from "./modules/detail.vue"
import { listYeePay, getYeePay, updateYeePayFeeRate } from "@/api/ecs/yeePay";
import { getUserProfile } from '@/api/system/user'
import { getAgentUserId } from '@/api/ecs/agent'

export default {
  name: "YeePay",
  components: { YeePayOperate, YeePayBank, YeePayDetail },
  data() {
    return {
      statusOptions: [
        { value: 1, label: "待提交" },
        { value: 2, label: "审核中" },
        { value: 3, label: "审核驳回" },
        { value: 4, label: "协议待签署" },
        { value: 5, label: "申请完成" },
      ],
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 易宝入网表格数据
      yeePayList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        requestId: null,
        signType: null,
        signName: null,
        shortName: null,
        legalName: null,
        contactMobile: null,
        contactEmail: null,
        status: null,
        platformNickName: null,
        agentNickName: null,
        childAgentNickName: null,
      },
      // 表单参数
      form: {},
      openBank: false,
      openProductCode: '',
      openAccountType: '',
      openDetail: false,
      feeRateTitle: '',
      feeRateForm: { id: 0, feeRate: 0.0 },
      feeRateVisible: false,
      user: {}

    };
  },
  created() {
    getUserProfile().then(response => {
      this.user = response.data;
      if (this.user.deptId === 101) {
        getAgentUserId(this.user.userId).then(response => {
          if (response.data.agentId !== 0) {
            this.user.deptId = 102
          }
        });
      }
    });
    this.getList();
  },
  methods: {
    /** 查询易宝入网列表 */
    getList() {
      this.loading = true;
      listYeePay(this.queryParams).then(response => {
        this.yeePayList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.form = null
      this.title = "添加商户入网";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids
      getYeePay(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商户入网";
      });
    },
    handleDetail(row) {
      const id = row.id || this.ids
      this.openDetail = true;
      this.form.id = row.id
    },
    close(val, val1) {
      this.open = val
      this.openBank = val
      this.openDetail = val
      if (val1) {
        this.getList()
      }
    },
    feeRateOpen(row) {
      this.feeRateTitle = `修改【${row.shortName}】费率`
      this.feeRateForm.id = row.id
      this.feeRateForm.feeRate = row.feeRate
      this.feeRateVisible = true
    },
    feeRateSubmit() {
      updateYeePayFeeRate(this.feeRateForm).then(res => {
        if (res.code === 200) {
          this.getList()
          this.feeRateVisible = false
        }
      })
    },
    feeRateCancel() {
      this.feeRateVisible = false
      this.$refs.feeRateRef.resetFields()
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ecs/yeePay/export', {
        ...this.queryParams
      }, `yeePay_${new Date().getTime()}.xlsx`)
    },
    handleBank(val) {
      this.form.requestId = val.requestId
      this.openProductCode = val.productCode
      this.openAccountType = val.signType
      this.openBank = true
    }
  }
};
</script>
